package com.foogui.primary.threadpool;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

public class FixedThreadPoolDemo {
    public static void main(String[] args) throws InterruptedException {
//        ExecutorService executorService = Executors.newFixedThreadPool(3);
        ExecutorService executorService = new ThreadPoolExecutor(5,10,10L, TimeUnit.SECONDS,new ArrayBlockingQueue<>(100),new DefaultThreadFactory("99"));
        CountDownLatch countDownLatch = new CountDownLatch(5);

        ReentrantLock lock = new ReentrantLock();
        for (int i = 0; i < 5; i++) {
            executorService.execute(()->{
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"正在执行");
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName()+"执行完毕");
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }finally {
                    lock.unlock();
                }

            });
        }

        countDownLatch.await();
        executorService.shutdown();
        System.out.println("线程池已关闭");
    }
}
